package com.bst.bsj.weixin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.homewaimai.common.lang.MD5Util;
import com.homewaimai.common.lang.SHAUtil;
import com.homewaimai.common.lang.StringUtil;

public class WeixinPayV3Util {

    public static final Log logger = LogFactory.getLog(WeixinPayV3Util.class);

    /**
     * 获取时间戳 unix
     * 
     * @return
     */
    public static String getUnixTimestamp() {

        Long timestamp = System.currentTimeMillis() / 1000L;
        String timestampstring = timestamp.toString();
        return timestampstring;
    }

    /**
     * MD5签名
     * 
     * @param kvs
     * @return
     */
    public static String MD5Sign(Map<String, String> kvs, String paternerKey) {

        List<String> keylist = new ArrayList<String>();
        Set<String> keyset = kvs.keySet();
        for (String item : keyset) {
            keylist.add(item);
        }

        Collections.sort(keylist);

        StringBuilder sb = new StringBuilder();

        // ascii 码从小到大排序，拼接字符
        for (String key : keylist) {

            String v = kvs.get(key);

            // 值不为空，则参与签名
            if (StringUtil.isNotBlank(v)) {
                sb.append(key).append("=").append(v).append("&");
            }
        }

        sb.append("key=").append(paternerKey);

        return MD5Util.getMD5UTF8String(sb.toString()).toUpperCase();
    }

    /**
     * 获取随即串
     * 
     * @return
     */
    public static String getNonceStr() {

        String my_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        int maxPos = my_chars.length();

        StringBuilder noceStr = new StringBuilder();
        for (int i = 0; i < 32; i++) {

            noceStr.append(my_chars.charAt((int) Math.floor(Math.random() * maxPos)));
        }

        return noceStr.toString();
    }

    /**
     * SHA签名
     * 
     * @param kvs
     * @return
     */
    public static String SHASign(Map<String, String> kvs) {

        List<String> keylist = new ArrayList<String>();
        Set<String> keyset = kvs.keySet();
        for (String item : keyset) {
            keylist.add(item);
        }

        Collections.sort(keylist);

        StringBuilder sb = new StringBuilder();

        // ascii 码从小到大排序，拼接字符
        for (String key : keylist) {

            String v = kvs.get(key);
            sb.append(key).append("=").append(v.toString()).append("&");

        }
        sb.deleteCharAt(sb.length() - 1);

        return SHAUtil.hex_sha1(sb.toString());

    }

    

    public static void main(String[] args) {

        // String t = getNonceStr();
        // System.out.println(t);
        //
        // String coding = System.getProperty("file.encoding");
        // System.out.println(coding);
        //
        // Map<String,String> kvs = new HashMap<String,String>();
        // kvs.put("appid", "wx52f246a72a70c04d");
        // kvs.put("mch_id", "1218812101");
        // kvs.put("nonce_str", "sjqHn0iMBNzfUxh3QxLYQFDOUiqM69Vf");
        // kvs.put("body", "墨意文化商品");
        // kvs.put("out_trade_no", "201503031010048464");
        // kvs.put("total_fee", "1");
        // kvs.put("spbill_create_ip", "221.224.214.98");
        // kvs.put("notify_url",
        // "http://www.moree.cn/pay_demo/wx_pay_notify_debug.htm");
        // kvs.put("trade_type", "JSAPI");
        // kvs.put("openid", "oVAznt00tndY9QrBrZjg8JnES0FU");
        // System.out.println(MD5Sign(kvs));

        // String a = "墨意文化商品aaa";
    }
}
